#ifndef SCREENSHOTWINDOW_H
#define SCREENSHOTWINDOW_H

#include "afxdef.h"
#include "screenshottool.h"
#include "drawproperty.h"
#include "picture.h"
#include "shape.h"
#include "colorpicker.h"

#include <QDialog>
#include <QFileDialog>
#include <QStack>


const int g_screenShotToolSpacing = 5;

namespace Ui {
class screenshotWindow;
}

typedef struct screenWindowStru
{
    QPixmap screenPixmapBackGround;
    QPixmap screenPixmap;
    QRect screenRect;
}screenWindow;

class ScreenshotWindow : public QDialog
{
    Q_OBJECT

public:

    enum {
      None,
      Select,
      Size,
      Move,
      Draw,
    };

    explicit ScreenshotWindow(QWidget *parent = nullptr);
    ~ScreenshotWindow();

    void showScreen();

    QRect FillShape();

    static QString getSaveFileName(QWidget *parent = nullptr,
                                       const QString &caption = QString(),
                                       const QString &dir = QString(),
                                       const QString &filter = QString(),
                                       QString *selectedFilter = nullptr,
                                       QFileDialog::Options options = QFileDialog::Options());


    static QUrl getSaveFileUrl(QWidget *parent = nullptr,
                                   const QString &caption = QString(),
                                   const QUrl &dir = QUrl(),
                                   const QString &filter = QString(),
                                   QString *selectedFilter = nullptr,
                                   QFileDialog::Options options = QFileDialog::Options(),
                                   const QStringList &supportedSchemes = QStringList());


Q_SIGNALS:
    void screenshotToolWidgetShow(bool b = true);
    void pasteOnTheScreen(const QPixmap &pixmap, const QRect &rect);

protected slots:
    void screenshotToolShow(bool b = true);
    void screenshotDone(bool toClipBoard);
    void savePicture();
    void pasteOnTheScreen();
    void drawNone();
    void drawRect();
    void drawText();
    void drawLine();
    void drawLineWithArrow();
    void drawShape(ScreenshotTool::shape shape);
    void PropertyChange(const DRAWPROPERTY& property);

    //撤销重做操作
    void Undo();
    void Redo();

protected:
    virtual void timerEvent(QTimerEvent *event) override;
    virtual bool eventFilter(QObject *watched, QEvent *event) override;
    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;
    virtual void keyPressEvent(QKeyEvent *) override;
    virtual void showEvent(QShowEvent *) override;
    virtual void enterEvent(QEvent *event) override;
    virtual void leaveEvent(QEvent *event) override;
    virtual void paintEvent(QPaintEvent *event) override;

    virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
    virtual void inputMethodEvent(QInputMethodEvent *event) override;

private:
    void init();
    QPoint adjustScreenshotToolPos();
    void calcRectPoint(QPoint &topLeft);
    //返回鼠标状态
    int detectCusor(const QPoint &pt, int &dir) const;

    Shape* createShape(int shape);
    //检查取色器位置是否在屏幕上
    PosQuadrant CheckPickerPos(const QPoint &pos);

    //检查是否超出左边界
    bool IsLeftBorder(int x, const QRect & rect) const
    {
        return x < rect.left();
    }
    //检查是否超出右边界
    bool IsRightBorder(int x, const QRect & rect) const
    {
        return x > rect.right();
    }
    //检查是否超出上边界
    bool IsTopBorder(int y, const QRect & rect) const
    {
        return y < rect.top();
    }
    //检查是否超出下边界
    bool IsBottomBorder(int y, const QRect & rect) const
    {
        return y > rect.bottom();
    }
    void TextEditComplete();

private:
    int m_cursorShape;
    Picture m_picture;
    //起始位置
    QPoint m_mousePressPos;
    QPoint m_topLeft;
    //结束位置
    QPoint m_mouseReleasePos;
    QPoint m_mouseCurPos;
    //鼠标形状
    int m_cursorMode;
    QPixmap m_allDesktopPixmap;
    QPixmap m_paintPixmap;
    QList<screenWindow> m_screenWindows;
    ScreenshotTool m_screenshotTool;
    CDrawProperty m_drawLineProperty;
    CDrawProperty m_drawRectProperty;
    CDrawProperty m_drawTextProperty;
    int m_curDir;
    bool m_bLeftButtonPress;

    //测试
    QList<Shape*> m_Shapes;
    Shape* m_pShape;
    int m_nShape;

    screenWindow m_screenWindow;
    DRAWPROPERTY m_property;

    //撤销存放图元
    QStack<Shape*> m_undoStack;

    //去色器放大镜
    CColorPicker m_colorPicker;
    //取色器显示的四种位置
    PosQuadrant m_curQuadrant;
    int m_nUpdateTimer;
};

#endif // SCREENSHOTWINDOW_H
